package android.slc.corelifecycle.domain;

import android.slc.core.domain.VmBox;
import android.slc.rxlifecycle.SimpleLifecycleScopeProvider;

import autodispose2.AutoDispose;
import autodispose2.AutoDisposeConverter;

/**
 * @author android.slc
 * @date 2021/4/2 12:47
 */
public class RxVmBox extends VmBox {
    protected SimpleLifecycleScopeProvider simpleLifecycleScopeProvider;
    protected boolean canStopLifecycleScope = false;

    /**
     * 设置 RxLifecycleDelegate
     *
     * @param simpleLifecycleScopeProvider
     */
    public void setRxLifecycleDelegate(SimpleLifecycleScopeProvider simpleLifecycleScopeProvider) {
        this.simpleLifecycleScopeProvider = simpleLifecycleScopeProvider;
    }

    /**
     * 绑定生命周期
     *
     * @param <T>
     * @return
     */
    public <T> AutoDisposeConverter<T> bindToLifecycle() {
        return AutoDispose.autoDisposable(simpleLifecycleScopeProvider);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (canStopLifecycleScope) {
            simpleLifecycleScopeProvider.stop();
        }
    }
}
